package com.example.play.repo

import com.example.common.entity.BaseResponse
import com.example.common.entity.CommendEntity
import com.example.common.entity.SimpleVideoEntity
import com.example.common.repo.BaseRepository
import com.example.http.OkHttpClientManager
import com.example.http.RetrofitManager
import com.example.play.api.PlayApiServer
import com.example.play.entity.AuthorEntity
import com.example.play.entity.DanmuEntity
import com.example.play.entity.MovieVideoEntity
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import okhttp3.RequestBody
import javax.inject.Inject

class PlayVideoRepository : BaseRepository<PlayApiServer>(){

    override fun createApiServer(): PlayApiServer {
        return RetrofitManager.getRetrofit(OkHttpClientManager.createOkHttpClient()).create(PlayApiServer::class.java)
    }
    suspend fun getAuthorById(userId: String): Flow<AuthorEntity?> {
        return flow {
            val result = apiServer.getAuthorById(userId)
            if (result.code == 200){
                emit(result.data)
            } else {
                emit(null)
            }
        }
    }

    //获取首页推荐视频
    suspend fun getRecommendSimpleVideo(page: Int, pageSize: Int): Flow<List<SimpleVideoEntity>>{
        return flow {
            val result = apiServer.getRecommendSimpleVideo(page, pageSize)
            if (result.code == 200){
                emit(result.data)
            }else{
                emit(emptyList())
            }
        }
    }

    //获取首页分类下视频
    suspend fun getSimpleVideoByChannelId(channelId: String,page: Int,pageSize: Int): Flow<List<SimpleVideoEntity>>{
        return flow {
            val result = apiServer.getSimpleVideoByChannelId(channelId, page, pageSize)
            if (result.code == 200){
                emit(result.data)
            }else{
                emit(emptyList())
            }
        }
    }

    //获取放映厅推荐视频
    suspend fun getRecommendMultiVideo(page: Int,pageSize: Int): Flow<List<MovieVideoEntity>>{
        return flow {
            val result = apiServer.getRecommendMultiVideo(page, pageSize)
            if (result.code == 200){
                emit(result.data)
            } else {
                emit(emptyList())
            }
        }
    }

    //获取放映厅分类下视频
    suspend fun getMultiVideoByTypeId(typeId: Int, page: Int, pageSize: Int): Flow<List<MovieVideoEntity>>{
        return flow {
            val result = apiServer.getMultiVideoByTypeId(typeId, page, pageSize)
            if (result.code == 200){
                emit(result.data)
            }else{
                emit(emptyList())
            }
        }
    }

    //获取视频评论
    suspend fun getCommentByUserId(dataType: Int, itemId: String): Flow<List<CommendEntity>>{
        return flow {
            val result = apiServer.getCommentByUserId(dataType, itemId)
            if (result.code == 200){
                emit(result.data)
            }else{
                emit(emptyList())
            }
        }
    }

    //发送评论
    suspend fun publishComment(body: RequestBody): Flow<CommendEntity?>{
        return flow {
            val result = apiServer.publishComment(body)
            if (result.code == 200){
                emit(result.data)
            }else{
                emit(null)
            }
        }
    }

    //发布弹幕
    suspend fun publishDanmuMsg(requestBody: RequestBody): Flow<String>{
        return flow {
            val result = apiServer.publishDanmuMsg(requestBody)
            if (result.code == 200){
                emit(result.data)
            } else {
                emit("")
            }
        }
    }

    //获取视频弹幕
    suspend fun getVideoDanmuMsg(dataType: Int, itemId: String): Flow<List<DanmuEntity>> {
        return flow {
            val result = apiServer.getVideoDanmuMsg(dataType, itemId)
            if (result.code == 200) {
                emit(result.data)
            } else {
                emit(emptyList())
            }
        }
    }




}